<?php
/**
 * 托运单
 *
 *
 */
class shipping_list extends Controller
{

	/**
	 * 构造函数
	 *
	 * 登陆检验
	 */
	function __construct()
    {
        parent::Controller(); 
		if (!$this->session->userdata('logged_in')){          		
			redirect('login');
			exit();
		}
    }
    
	// --------------------------------------------------------------------

    /**
	 * 托运单列表
	 *
	 *
	 */	
    function index()
    {

        // 接收客户端反馈的信息： ajax，post
		$field = ($this->input->post('field')) ? ($this->input->post('field')) : 0;
		$keyword = ($this->input->post('keyword')) ? $this->input->post('keyword') : 0;
		$search = $this->input->post('search');
		$page = $this->input->post('page'); $page = (int)$page;
        
		// 分页+查询的url设置
        $segments = $this->uri->uri_to_assoc();
        
		if ($search){
			$segments['field'] = $field;
			$segments['keyword'] = $keyword;			
		}else{
			$segments['field'] = !empty($segments['field']) ? $segments['field'] : 0;
			$segments['keyword'] = !empty($segments['keyword']) ? $segments['keyword'] : 0;
		}			       	
		$search_uri = '/' . $segments['field'] . '/' . $segments['keyword'];
		
		
		 
		//计算查询条件
        $options = array(
            'conditions' => null,
        );


        $data['keyword'] = '';
         
        
 
        //  按关键字查询
		if (!empty($segments['keyword'])){
           
                $options['conditions'][$segments['field']] = $segments['keyword'] ;
                $data['keyword'] =$segments['keyword'];
           
        }

    
	    // 分页的开始数据行：$page_offset
		if($page){
            $page_offset = $page-1;
		}else if (!empty($segments['page'])){
            $page_offset = (int)$segments['page'];		
        } else {
            $page_offset = 0;
        }
		if ($page_offset<0){
            $page_offset=0;
		}
		
		// 分页url
		$base_url = site_url('shipping_list/index/page') . '/';

		// 每页显示数据条数
        $per_page = 14;
        
		// 数据总数
		$this->load->model('shipping_list_model');	
			
		$data['total_rows'] =  $this->shipping_list_model->count_traders($options);
 		 
		// 总页数
        $data['total_pages'] = ceil($data['total_rows']/$per_page);
        
		// 当前页（input）
        $data['page'] = $page_offset+1;
		$data['page'] = ($data['page']>$data['total_pages'] && $data['total_pages']>0) ? $data['total_pages'] : $data['page'];

		// 第一页 最后一页 前一页 后一页
		$data['page_first'] = $base_url.'0'.$search_uri;
		$data['page_last'] = $base_url.($data['total_pages']-1).$search_uri;
		$data['page_prev'] = ($data['page']>1) ? $base_url.($data['page']-2).$search_uri : $data['page_first'];
	    $data['page_next'] = ($data['page']<$data['total_pages']) ? $base_url.($data['page']).$search_uri : $data['page_last'];

		// 当前页
		$data['cur_page'] =  $base_url.($data['page']-1).$search_uri;
		
		// 当前页起始数据号
		$data['show_start'] = ($data['page']-1)*$per_page+1;

		// 当前页结束数据号
		$data['show_end'] = ($page_offset<$data['total_pages']-1) ? ($data['show_start']+$per_page-1) : $data['total_rows'];

	    // 托运单结果集
		$data['shipping_list'] = $this->shipping_list_model->find_traders($options, $per_page, ($data['page']-1)*$per_page);
       
		// 发站代码
		$this->load->model('code_checkedin_site_model');
		$data['checkedin_site'] = $this->code_checkedin_site_model->get_all();
		// 到站代码
		$this->load->model('code_destination_site_model');
		$data['destination_site'] = $this->code_destination_site_model->get_all();
		
        $this->firephp->log($data);
	    //$data['editing']['indexed_org_string'] = $this->code_indexed_org_model->get_list($data['editing']['indexed_organization']);              
        
		// ajax 
		if (!empty($segments['ajax']) || $search==1){
			$this->load->view('lspf_trader/lspf_trader/ajax_list',$data);
		// 非 ajax
		}else{
            $this->load->view('lspf_trader/lspf_trader/list',$data);
		}
    }
    
	// --------------------------------------------------------------------

    /**
	 * 添加托运单
	 *
	 *
	 */	  
	function add()
    {
        $this->edit();
    }
    
	// --------------------------------------------------------------------

    /**
	 * 编辑托运单
	 *
	 *
	 */	  
	function edit()
    {
		// 检验操作权限
		if (!admin_priv('trader_manage')){
			return show_message2('你没有此项操作的权限!', 'shipping_list');
		}

		$params = $this->uri->uri_to_assoc();
        if (!empty($params['id']) && $params['id'] > 0){
            $id = $params['id'];
            $this->load->model('shipping_list_model');
            $data['editing'] = $this->shipping_list_model->load($id);
            $this->firephp->log($data);
			if (!$data['editing']){
                return show_message2('无效ID:'.$id, 'shipping_list');
            }
            
 
	 
			        
			$data['header_text'] = '编辑托运单(ID:'.$data['editing']['id'].')';
        } else {

 

            $data['editing'] = array(

                'id' => null,
                'trader_name' => null,
                'trader_leader' => null, 
				'telephone' => null,  
				'address' => null,    
				'square' => null,
            	'license' =>null,
                'record_date' => date('Y年m月d日 HH:MM:SS',time()),
            	'demo' => null
			    	 
 				
            );
		 

			$data['header_text'] = '添加托运单';
        }
      
	 
           
 
       // 发站代码
       $this->load->model('code_checkedin_site_model');
       $data['checkedin_site'] = $this->code_checkedin_site_model->get_all();        
       // 到站代码
       $this->load->model('code_destination_site_model');
       $data['destination_site'] = $this->code_destination_site_model->get_all();

       // 付款方式代码
       $this->load->model('code_payment_mode_model');
       $data['payment_mode'] = $this->code_payment_mode_model->get_all();
        
       // 服务方式代码
       $this->load->model('code_delivery_methmod_model');
       $data['delivery_methmod'] = $this->code_delivery_methmod_model->get_all();
       
              
       
       //开票人列表
        $this->load->model('admin_user_model');
       $data['user'] = $this->admin_user_model->get_all();
        
       $this->load->view('lspf_trader/lspf_trader/edit',$data);

    }
    
    // --------------------------------------------------------------------

    /**
	 * 提交数据
	 *
	 *
	 */	  
    function save()
    {

    	 
  
	    
    	// 保存后并继续编辑信号
		$re_edit = $this->input->post('re_edit');

        // 托运单id
        $id = $this->input->post('id');
        
		// 基本信息
        $trader_name            = $this->input->post('trader_name');
        $trader_leader          = $this->input->post('trader_leader');
		$telephone              = $this->input->post('telephone');
	    $address                = $this->input->post('address');
	    $square					= $this->input->post('square');
	    $license				= $this->input->post('license');
        $record_date            = $this->input->post('record_date');
        $demo                   = $this->input->post('demo');	    
	   
    
	    // 加载表单验证类
        $this->load->library('validation');


        
		// 如果提交数据符合所设置的规则，则继续运行
		 
 
			//把数据提交给模型
            $this->load->model('shipping_list_model');         
            $this->shipping_list_model->trader_name = $trader_name;
            $this->shipping_list_model->trader_leader = $trader_leader;
			$this->shipping_list_model->telephone = $telephone;
			$this->shipping_list_model->address = $address;
            $this->shipping_list_model->square = $square;
            $this->shipping_list_model->license = $license;
            $this->shipping_list_model->record_date = $record_date;
            $this->shipping_list_model->demo = $demo;        
 
            
 
            
			 

			//更新
            if ($id){
                $old_pro = $this->shipping_list_model->load($id);
                $this->shipping_list_model->update($id);
				$new_pro = $this->shipping_list_model->load($id);

			 
		          
				//继续编辑
				if($re_edit){ 
				   //echo $id;
                    show_message2('"托运单(ID:'.$id.')" 已更新!', 'lspf_trader/edit/id/'.$id);
			    //返回列表
			    }else{ 
				    //echo '更新成功';
					show_message2('"托运单(ID:'.$id.')" 已更新!', 'shipping_list');
				}
				
            // 添加新托运单
            } else {
                $this->shipping_list_model->create();
                

					 

			
			   if ($re_edit){
				   //echo $newly_one['id'];
                   show_message2('"托运单(ID:'.$newly_one['id'].')" 已保存!', 'lspf_trader/edit/id/'.$newly_one['id']);
			   }else{
				   //echo '保存成功!';
				    show_message2('保存成功!', 'shipping_list');
			   }
			}
   
		
		 
				
	}

    // --------------------------------------------------------------------

    /**
	 * 删除托运单
	 *
	 *
	 */	
    function delete()
    {
		 // 检验操作权限
		if (!admin_priv('trader_delete')){
			return show_message2('你没有此项操作的权限!', 'shipping_list');
		}

		$params = $this->uri->uri_to_assoc();
        if (isset($params['id']) && ($id = $params['id']) > 0){

		 

			// 删除托运单
            $this->load->model('shipping_list_model');
            if ($this->shipping_list_model->delete($id)){
                //echo $id;
				show_message2('"托运单(ID:'.$id.')" 已被删除!', 'shipping_list');
            } else {
                //return false;
				return show_message2('无效ID:'.$id, 'shipping_list');
            }
        }
	}

    // --------------------------------------------------------------------

 
 
	
 	/**
 	 * 表单UID字段检查
 	 * 大于0返回TRUE
 	 *否则返回FALSE
 	 */
     function uid_check($str){
     	$this->firephp->log("我执行了");
     	
     	if ((int)$str > 0){
     		return TRUE;
     	}else 
     	{
     		$this->validation->set_message('uid_check','数组');
     		return FALSE;
     	}
     }

 

	

	// --------------------------------------------------------------------

    /**
	 * 托运单列表的单个字段编辑  ajax
	 *
	 */	
	function list_toggle()
	{
        $segments = $this->uri->uri_to_assoc();
		$field = $segments['field'];
		$trader_id = (int)$segments['id'];
		$val = $segments['val'];
		$this->load->model('shipping_list_model');
        if ($value = $this->shipping_list_model->update_one(array($field => $val),$trader_id)){
			echo $value;
		}
	}
	
	 
	   
}